package org.light.generator;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.Statement;
import org.light.domain.StatementList;
import org.light.exception.ValidateException;
import org.light.utils.WriteableUtil;

public class DateUtilGenerator extends Generator{	
    protected Domain sampleDomain;

	public Domain getSampleDomain() {
        return sampleDomain;
    }

    public void setSampleDomain(Domain sampleDomain) {
        this.sampleDomain = sampleDomain;
    }

    public DateUtilGenerator(){
		super();		
		this.standardName = "DateUtil";
		this.fileName = "DateUtil.java";
	}
	
	@Override
	public StatementList generateStatementList() throws ValidateException {
    	List<Writeable> sList = new ArrayList<Writeable>();
        sList.add(new Statement(1000L,0,"package "+this.sampleDomain.getPackageToken()+".utils;"));
        sList.add(new Statement(2000L,0,""));
        sList.add(new Statement(3000L,0,"import java.text.ParseException;"));
        sList.add(new Statement(4000L,0,"import java.text.SimpleDateFormat;"));
        sList.add(new Statement(5000L,0,""));
        sList.add(new Statement(6000L,0,"public class DateUtil {"));
        sList.add(new Statement(7000L,0,""));
        sList.add(new Statement(8000L,1,"private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat(\"yyyy-MM-dd\");"));
        sList.add(new Statement(9000L,1,"private static final SimpleDateFormat SIMPLE_DATE_TIME_FORMAT = new SimpleDateFormat(\"yyyy-MM-dd HH:mm:ss\");"));
        sList.add(new Statement(10000L,1,"// 字符串转换为java.util.Date类型日期时间"));
        sList.add(new Statement(11000L,1,"public static java.util.Date strDateToUtilDate(String strDate) {"));
        sList.add(new Statement(12000L,2,"try {"));
        sList.add(new Statement(13000L,3,"if (StringUtil.isBlank(strDate)) return null;"));
        sList.add(new Statement(14000L,3,"else return SIMPLE_DATE_FORMAT.parse(strDate);"));
        sList.add(new Statement(15000L,2,"} catch (ParseException e) {"));
        sList.add(new Statement(16000L,3,"e.printStackTrace();"));
        sList.add(new Statement(17000L,2,"}"));
        sList.add(new Statement(18000L,2,"return null;"));
        sList.add(new Statement(19000L,1,"}"));
        sList.add(new Statement(20000L,1,""));
        sList.add(new Statement(21000L,1,"// 字符串转换为java.util.Date类型日期时间"));
        sList.add(new Statement(22000L,1,"public static java.util.Date strDateTimeToUtilDateTime(String strDate) {"));
        sList.add(new Statement(23000L,3,"try {"));
        sList.add(new Statement(24000L,4,"if (StringUtil.isBlank(strDate)) return null;"));
        sList.add(new Statement(25000L,4,"else return SIMPLE_DATE_TIME_FORMAT.parse(strDate);"));
        sList.add(new Statement(26000L,3,"} catch (ParseException e) {"));
        sList.add(new Statement(27000L,4,"e.printStackTrace();"));
        sList.add(new Statement(28000L,3,"}"));
        sList.add(new Statement(29000L,3,"return null;"));
        sList.add(new Statement(30000L,2,"}"));
        sList.add(new Statement(31000L,0,""));
        sList.add(new Statement(32000L,1,"// java.util.Date类型日期时间转换为java.sql.Date类型日期时间"));
        sList.add(new Statement(33000L,1,"public static java.sql.Date utilDateToSqlDate(java.util.Date date) {"));
        sList.add(new Statement(34000L,2,"// long date.getTime():返回自 1970 年 1 月 1 日 00:00:00 GMT以来此 Date对象表示的毫秒数"));
        sList.add(new Statement(35000L,2,"if (date != null) {"));
        sList.add(new Statement(36000L,3,"return new java.sql.Date(date.getTime());"));
        sList.add(new Statement(37000L,2,"} else {"));
        sList.add(new Statement(38000L,3,"return null;"));
        sList.add(new Statement(39000L,2,"}"));
        sList.add(new Statement(40000L,1,"}"));
        sList.add(new Statement(41000L,1,""));
        sList.add(new Statement(42000L,1,"// java.util.Date类型日期时间转换为java.sql.Date类型日期时间"));
        sList.add(new Statement(43000L,1,"public static java.util.Date sqlDateToUtilDate(java.sql.Date date) {"));
        sList.add(new Statement(44000L,2,"if (date != null) {"));
        sList.add(new Statement(45000L,3,"// long date.getTime():返回自 1970 年 1 月 1 日 00:00:00 GMT以来此 Date对象表示的毫秒数"));
        sList.add(new Statement(46000L,3,"return new java.util.Date(date.getTime());"));
        sList.add(new Statement(47000L,2,"} else {"));
        sList.add(new Statement(48000L,3,"return null;"));
        sList.add(new Statement(49000L,2,"}"));
        sList.add(new Statement(50000L,1,"}"));
        sList.add(new Statement(51000L,2,""));
        sList.add(new Statement(52000L,1,"// java.util.Date类转换为字符串类型"));
        sList.add(new Statement(53000L,1,"public static String utilDateToString(java.util.Date date) {"));
        sList.add(new Statement(54000L,2,"return SIMPLE_DATE_FORMAT.format(date);"));
        sList.add(new Statement(55000L,1,"}"));
        sList.add(new Statement(56000L,1,""));
        sList.add(new Statement(57000L,1,"// java.util.Date类转换为字符串类型"));
        sList.add(new Statement(58000L,1,"public static String utilDateTimeToString(java.util.Date date) {"));
        sList.add(new Statement(59000L,2,"return SIMPLE_DATE_TIME_FORMAT.format(date);"));
        sList.add(new Statement(60000L,1,"}"));
        sList.add(new Statement(61000L,0,"}"));        

    	return WriteableUtil.merge(sList);
	}
}
